Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: resize oversize images before sending #235

Merged
merged 15 commits into from
Aug 5, 2024

Conversation

SkyTNT
Copy link
Contributor

@SkyTNT SkyTNT commented Jul 30, 2024

添加自动缩小过大图片功能

Copy link

github-actions bot commented Jul 30, 2024

PR Health

Build ✅

Details
esbuild: packages/core/src/index.ts -> packages/core/lib/index.js
esbuild: packages/danbooru/src/index.ts -> packages/danbooru/lib/index.js
esbuild: packages/e621/src/index.ts -> packages/e621/lib/index.js
esbuild: packages/gelbooru/src/index.ts -> packages/gelbooru/lib/index.js
esbuild: packages/konachan/src/index.ts -> packages/konachan/lib/index.js
esbuild: packages/local/src/index.ts -> packages/local/lib/index.js
esbuild: packages/lolibooru/src/index.ts -> packages/lolibooru/lib/index.js
esbuild: packages/lolicon/src/index.ts -> packages/lolicon/lib/index.js
esbuild: packages/moehu/src/index.ts -> packages/moehu/lib/index.js
esbuild: packages/pixiv/src/index.ts -> packages/pixiv/lib/index.js
esbuild: packages/safebooru/src/index.ts -> packages/safebooru/lib/index.js
esbuild: packages/sankaku/src/index.ts -> packages/sankaku/lib/index.js
esbuild: packages/yande/src/index.ts -> packages/yande/lib/index.js

Lint ✅

Details
Checking formatting...
All matched files use Prettier code style!

packages/core/src/index.ts Outdated Show resolved Hide resolved
packages/core/src/index.ts Outdated Show resolved Hide resolved
packages/core/src/index.ts Outdated Show resolved Hide resolved
packages/core/src/index.ts Show resolved Hide resolved
packages/core/src/index.ts Outdated Show resolved Hide resolved
packages/core/src/command.tsx Outdated Show resolved Hide resolved
packages/core/src/index.ts Outdated Show resolved Hide resolved
packages/core/src/command.tsx Outdated Show resolved Hide resolved
packages/core/src/index.ts Outdated Show resolved Hide resolved
@Lipraty
Copy link
Member

Lipraty commented Jul 30, 2024

Thank you very much for your contribution. Here are some issues I noticed:

  • There is no management of side effects for the package using the Node addon, which can disrupt Koishi's HMR and subsequent update operations.
  • The resizing method should be independent rather than coupled with the toBase64 method.
  • Please respect the original coding style.

Some thoughts on potential solutions:

  • For packages with strong platform dependencies like Node addon/gyp, try to avoid using them and instead opt for packages written purely in JavaScript or manage the Node addon.
  • toBase64 is just one method of sending images for booru and should not be forcibly coupled.

Reference:

https://koishi.chat/zh-CN/cookbook/design/storage.html#%E5%BA%94%E5%BD%93%E9%81%BF%E5%85%8D%E7%9A%84%E6%83%85%E5%86%B5

https://forum.koishi.xyz/t/topic/1930

@SaarChaffee
Copy link
Member

SaarChaffee commented Jul 30, 2024

另外,有部分 api 支持返回不同尺寸或大小的图片。如果有,应当优先使用 api 所提供的。

@SkyTNT
Copy link
Contributor Author

SkyTNT commented Jul 30, 2024

另外,有部分 api 支持返回不同尺寸或大小的图片。如果有,应当优先使用 api 所提供的。

原本逻辑应该就是优先使用 api 所提供的大小吧。我只是加了如果超过选择的大小就自动缩放,这是可选的。

但我之前用的时候发现就算选了large,meidum,图片有时候都特别大超过15mb,图源是pixiv,lolicon。

packages/core/src/source.ts Outdated Show resolved Hide resolved
@SkyTNT
Copy link
Contributor Author

SkyTNT commented Jul 30, 2024

现在再看看怎么样

@SkyTNT
Copy link
Contributor Author

SkyTNT commented Jul 30, 2024

jimp存在内存和性能问题,考虑再看看有没有其它可以用的库
jimp-dev/jimp#153
jimp-dev/jimp#915

有时会出现maxMemoryUsageInMB limit exceeded by at least 10MB.的报错

@SkyTNT
Copy link
Contributor Author

SkyTNT commented Jul 30, 2024

明天试试用canvas能不能实现

@MaikoTan
Copy link
Member

MaikoTan commented Jul 31, 2024

明天试试用canvas能不能实现

Use canvas might be a better idea since many users have it installed.

Since there are image read / write libraries like pngjs and jpeg-js, it is also not hard to just implement a resize algorithm based on similarity between pixels though.

@MaikoTan
Copy link
Member

By the way, you should also add this option into the document, in this case, it would go to the page https://github.com/koishijs/koishi-plugin-booru/blob/main/docs/zh-CN/config.md

@github-actions github-actions bot added the 📝 documentation Improvements or additions to documentation label Jul 31, 2024
@SkyTNT SkyTNT requested a review from MaikoTan July 31, 2024 13:23
@SkyTNT
Copy link
Contributor Author

SkyTNT commented Jul 31, 2024

现在应该可以了吧

@SkyTNT
Copy link
Contributor Author

SkyTNT commented Jul 31, 2024

等等,似乎还要优化一下,我看puppeteer的 toBuffer是基于 toDataURL实现的,多了不必要的base64转换

@SkyTNT
Copy link
Contributor Author

SkyTNT commented Jul 31, 2024

应该可以了

@SkyTNT SkyTNT requested a review from SaarChaffee July 31, 2024 14:52
Copy link
Member

@MaikoTan MaikoTan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now it should be good to merge

@Lipraty make a double check please.

@MaikoTan MaikoTan changed the title feature: add auto resize feat: auto resize oversize images before sending Aug 5, 2024
@MaikoTan MaikoTan changed the title feat: auto resize oversize images before sending feat: resize oversize images before sending Aug 5, 2024
Copy link
Member

@Lipraty Lipraty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@MaikoTan MaikoTan added this pull request to the merge queue Aug 5, 2024
Merged via the queue into koishijs:main with commit 6da0d7f Aug 5, 2024
5 checks passed
@SkyTNT SkyTNT deleted the add-resize branch August 5, 2024 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⚙️ core 📝 documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants